﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.Runtime.Caching;
using System.Collections;
using DataService;
using System.Configuration;

namespace WCFService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MemoryCacheService : IMemoryCacheService
    {
        private bool IsDirty(string id)
        {
            return MemoryCache.Default["fingersCollection"] == null || 
                    MemoryCache.Default["dirty"] == null ||
                    "true" == MemoryCache.Default["dirty"] as string ||
                    id != MemoryCache.Default["id"] as String;
        }

        public void SetDirty()
        {
            MemoryCache.Default["dirty"] = "true";
        }

        public byte[] GetImage(IMAGE_TYPE imageType, int id)
        {
            byte[] buffer = new byte[1];
            ArrayList fingersCollection = null;

            if (getAppSetting("Enroll") == "db")
            {
                var ds = new DatabaseService();

                if (imageType == IMAGE_TYPE.wsq)
                {
                    buffer = ds.GetImage(IMAGE_TYPE.wsq, Convert.ToInt32(id));
                    var bioProcessor = new BioProcessor.BioProcessor();
                    bioProcessor.processEnrolledData(buffer, out fingersCollection);

                }
                else if (imageType == IMAGE_TYPE.picture)
                {
                    buffer = ds.GetImage(IMAGE_TYPE.picture, Convert.ToInt32(id));
                }
            }

            return buffer;

        }

        public bool Contains(string id)
        {
            if (MemoryCache.Default["fingersCollection"] == null || id != MemoryCache.Default["id"] as String)
            {
                if (MemoryCache.Default["fingersCollection"] != null)
                {
                    MemoryCache.Default.Remove("fingersCollection");
                    MemoryCache.Default.Remove("id");
                }
                return false;
            }
            else
                return true;
        }

        ArrayList fingersCollection = null;

        public byte[] GetRawFingerCollection(string id)
        {
            byte[] buffer = new byte[1];
            //ArrayList fingersCollection = null;

            if (getAppSetting("Enroll") == "db")
            {
                var ds = new DatabaseService();
                buffer = ds.GetImage(IMAGE_TYPE.wsq, Convert.ToInt32(id));
                //var bioProcessor = new BioProcessor.BioProcessor();
                //bioProcessor.DeserializeWSQArray(buffer, out fingersCollection);
            }

            return buffer;
        }

        public ArrayList GetQualityFingerCollection(string id)
        {
            if (IsDirty(id))
            {
                byte[] buffer = new byte[1];
                //ArrayList fingersCollection = null;

                if (getAppSetting("Enroll") == "db")
                {
                    var ds = new DatabaseService();
                    buffer = ds.GetImage(IMAGE_TYPE.wsq, Convert.ToInt32(id));
                    var bioProcessor = new BioProcessor.BioProcessor();
                    bioProcessor.processEnrolledData(buffer, out fingersCollection);
                    MemoryCache.Default["id"] = id;
                    MemoryCache.Default["fingersCollection"] = fingersCollection;
                    MemoryCache.Default["dirty"] = "false";
                }

                return fingersCollection;
            }
            else
                return MemoryCache.Default["fingersCollection"] as ArrayList;
        }

        public byte[] GetPicture(string id)
        {
            if (getAppSetting("Enroll") == "db")
            {
                var ds = new DatabaseService();
                return ds.GetImage(IMAGE_TYPE.picture, Convert.ToInt32(id));
            }
            else
            {
                return new byte[0];
            }
        }

        public void Put(FingerPrintDataContract fingersCollectionDataContract)
        {
            MemoryCache.Default["id"] = fingersCollectionDataContract.id;
            MemoryCache.Default["fingersCollection"] = fingersCollectionDataContract.fingersCollection;
            MemoryCache.Default["dirty"] = "false";
        }

        internal static string getAppSetting(string key)
        {
            var setting = ConfigurationManager.AppSettings[key];
            //throw new Exception(setting);

            // If we didn't find setting, try to load it from current dll's config file
            if (string.IsNullOrEmpty(setting))
            {
                var filename = System.Reflection.Assembly.GetExecutingAssembly().Location;
                var configuration = ConfigurationManager.OpenExeConfiguration(filename);
                if (configuration != null)
                    setting = configuration.AppSettings.Settings[key].Value;
            }

            return setting;
        }
    }
}
